package com.qiangesoft.sms.tencentcloud.config;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 腾讯云短信配置
 *
 * @author qiangesoft
 * @date 2024-04-30
 */
@Configuration
@EnableConfigurationProperties(TencentcloudSmsProperties.class)
@ConditionalOnProperty(prefix = "sms.tencentcloud", name = "enabled", havingValue = "true")
public class TencentcloudSmsConfiguration {

    private static final String ENDPOINT = "sms.tencentcloudapi.com";

    @Autowired
    private TencentcloudSmsProperties tencentcloudSmsProperties;

    @Bean
    @ConditionalOnMissingBean
    public SmsClient smsClient() {
        // 实例化一个认证对象
        Credential cred = new Credential(tencentcloudSmsProperties.getSecretId(), tencentcloudSmsProperties.getSecretKey());

        // 实例化一个http选项，可选
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setReqMethod("POST");
        httpProfile.setConnTimeout(60);
        httpProfile.setEndpoint(ENDPOINT);

        // 实例化一个客户端配置对象
        ClientProfile clientProfile = new ClientProfile();
        // SDK默认用TC3-HMAC-SHA256进行签名，非必要请不要修改这个字段
        clientProfile.setSignMethod("HmacSHA256");
        clientProfile.setHttpProfile(httpProfile);
        return new SmsClient(cred, tencentcloudSmsProperties.getRegionId(), clientProfile);
    }

}
